package com.project.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.project.demo.controller.base.BaseController;
import com.project.demo.dao.LiveCourseMapper;
import com.project.demo.entity.LiveCourse;
import com.project.demo.service.LiveCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Map;

/**
 * 直播讲座：(live_course)表控制层
 */
@RestController
@RequestMapping("live_course")
public class LiveCourseController extends BaseController<LiveCourse, LiveCourseService> {

    @Autowired
    public LiveCourseController(LiveCourseService service) {
        setService(service);
    }

    @Autowired
    LiveCourseMapper liveCourseMapper;

    @PostMapping("/update")
    @Transactional
    public Map<String, Object> update(@RequestBody LiveCourse liveCourse) throws IOException {
        LambdaQueryWrapper<LiveCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LiveCourse::getCourseId, liveCourse.getCourseId());
        liveCourse.setReservedNum(liveCourse.getReservedNum() + 1);
        int update = liveCourseMapper.update(liveCourse, wrapper);
        if (update == 1) {
            return success(1);
        }
        return success(0);
    }

}
